<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>系统日志</title>
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/assets/module/admin.css?v=315"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">用户名：</label>
                        <div class="layui-input-inline mr0">
                            <input name="username" class="layui-input" id="demoReload" placeholder="请输入查询用户名">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">日期：</label>
                        <div class="layui-input-inline mr0">
                            <input class="layui-input date-icon" id="timearea" placeholder="请选择查询时间段" type="text" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" data-type="reload" lay-submit >
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="userLog" lay-filter="tableLog"></table>
        </div>
    </div>
</div>
<script id="dateTpl" type="text/html">
    {{#  var fn = function(){
    return moment(d.ApplyDate).format("YYYY-MM-DD");
    }; if(true){ }}
    {{ fn() }}
    {{#  } }}

</script>
<script type="text/html" id="toolbarDemo">
    <shiro:hasPermission name="login_log:delete">
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除选中数据</button>
    </shiro:hasPermission>
    <shiro:lacksPermission name="login_log:delete">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-danger" disabled>无权限</button>
        </div>
    </shiro:lacksPermission>
</script>
<script type="text/html" id="barDemo">
    <shiro:hasPermission name="login_log:delete">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </shiro:hasPermission>
    <shiro:lacksPermission name="login_log:delete">
        <a class="layui-btn layui-btn-danger layui-btn-xs">无权限</a>
    </shiro:lacksPermission>
</script>
<!-- js部分 -->
<script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/assets/js/common.js?v=315"></script>
<script>
    layui.use(['laydate','table','form','notice'], function () {
        var laydate = layui.laydate,
        table=layui.table,
        form=layui.form,
        notice=layui.notice;
        //时间选择器
        laydate.render({
            elem: '#timearea'
            , range: true
        });

        var talbeIns=table.render({
            elem: '#userLog'
            ,url:'/loginLog/list'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: [ 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '刷新'
                ,layEvent: 'reset'
                ,icon: 'layui-icon-refresh'
            }]
            ,cols: [
                [
                    {type: 'checkbox', fixed: 'left',align:'center'}
                    ,{field:'username',  title: '登录用户',align:'center'}
                    ,{field:'ip', title: 'IP地址',align:'center'}
                    ,{field:'location', title: '登录地点',align:'center'}
                    ,{field:'loginTimeStr', title: '登录时间', sort: true,align:'center'}
                    ,{field:'logSystem', title: '登录系统',align:'center'}
                    ,{field:'browser', title: '浏览器',align:'center'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo',align:'center'}
                ]
            ]
            ,page: true
            ,limit:10
            , limits: [ 5, 10, 15, 20,25]
            , loading: true
        });

        //监听行工具事件
        table.on('tool(tableLog)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除此数据？',{icon:3, title:'提示信息',skin: 'layui-layer-admin'},function(index){
                    $.get('/loginLog/del/'+obj.data.id,function(data){
                        if (data.code==200){
                            talbeIns.reload();
                            layer.close(index);
                            notice.msg("删除成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                        } else{
                            notice.msg(data.message,{icon:2,animateInside:true,timeout:2000});
                        }
                    });
                });
            }
        });
        //头工具栏事件
        table.on('toolbar(tableLog)', function(obj){
            if (obj.event==='delete'){
                var checkStatus = table.checkStatus(obj.config.id);
                if (!checkStatus.data.length){
                    notice.msg("请选择要删除的数据！！",{icon:5,timeout:2000,animateInside:true,audio:'1'});
                }else{
                    layer.confirm('确定删除多条数据？',{icon:3, title:'提示信息',skin: 'layui-layer-admin'},function(index){
                        var logIds = [];
                        layui.each(checkStatus.data, function (key, item) {
                            logIds.push(item.id);
                        });
                        $.get('/loginLog/delAll/'+logIds,function(data){
                            if (data.code==200){
                                talbeIns.reload();
                                layer.close(index);
                                notice.msg("删除成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                            } else{
                                notice.msg(data.message,{icon:2,animateInside:true,timeout:2000});
                            }
                        });
                    });
                }
            }else if(obj.event==='reset'){
                talbeIns.reload();
            }

        });

        //表单刷新
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');
                var timeArea = $("#timearea").val();
                var startTime = "";
                var endTime = "";
                if (timeArea) {
                    startTime = timeArea.split(" - ")[0];//开始时间
                    endTime = timeArea.split(" - ")[1];//结束时间
                }
                //执行重载
                table.reload('userLog', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        username: demoReload.val(),
                        startTime:startTime,
                        endTime:endTime
                    }
                }, 'data');
            }
        };
        //点击搜索按钮
        form.on('submit(formSubSearchLog)',function (data) {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

</script>
</body>
</html>